package com.baseframe.tool.utils.encrypt;

import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;

public class Argon2Util {

    private static final Argon2 ARGON2 = Argon2Factory.create(Argon2Factory.Argon2Types.ARGON2id);

    private static final int ITERATIONS = 2;//迭代次数
    private static final int MEMORY= 65536;//内存
    private static final int PARALLELISM = 1;//并行度


    public static String encode(String rawStr) {

        return ARGON2.hash(ITERATIONS, MEMORY, PARALLELISM, rawStr.toCharArray());
    }


    public static boolean matches(String encodedStr,String rawStr) {
        return ARGON2.verify(encodedStr, rawStr.toCharArray());
    }

    public static boolean addPrefix2Matches(String encodedStr2Data,String rawStr){

        return matches(possessPrefix2EncodedStr(encodedStr2Data),rawStr);
    }
    //获取前缀
    private static String getPrefix(){
        return "$argon2id" +
                "$v=19" +
                "$m=" +
                MEMORY +
                ",t=" +
                ITERATIONS +
                ",p=" +
                PARALLELISM +
                "$";
    }
//拥有
    public static String possessPrefix2EncodedStr(String encodedStr){
        return getPrefix()+encodedStr;
    }

    public static String noPrefixEncoded(String rawStr){
        String encode = encode(rawStr);
        return encode.substring(getPrefix().length());
    }

}
